<template>
    <div class="login">
      <div class="r-lg">
        <el-form :model="loginForm" status-icon :rules="rules" ref="loginForm" label-width="80px" class="demo-ruleForm">

          <el-form-item label="手机" prop="phone">
            <el-input type="text" v-model="loginForm.phone" autocomplete="on"></el-input>
          </el-form-item>
      
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginForm.password" autocomplete="on"></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')">提交</el-button>
           
          </el-form-item>
        </el-form >
      </div>


    </div>
</template>

<script>
import axios from "../utils/axios";

export default {
  data() {

    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入手机号'));
      } else {

        callback();
      }
    };

    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else {

        callback();
      }
    };


    return {
      loginForm: {
        password: '',
   
        phone:''
      },
      rules: {
        password: [
          { validator: validatePass2, trigger: 'blur' }
        ],
    
        phone: [
          { validator: validatePass, trigger: 'blur' }
        ]


      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {


            axios({
              method:"post",
              url:"/api/login",
              data:this.loginForm


            }).then(
                (response)=>{
                  if(response.code==200){
                    console.log("北京2008")
                    console.log(response)
                    localStorage.setItem("token",JSON.stringify(response.list.token))
                    console.log(response)
                    this.$message.success("恭喜你登录成功")
                    this.$router.push("/main")
                  }else{
                    this.$message.error(`登录失败: ${response.msg}`)
                  }
                }
            )




          }else{
            // this.$refs.rules[0].message="请输入正确的手机号码"
            this.$message.error("请输入账号和密码")
          }


      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>
<style scoped>
.login {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100vh;
  background-color: rgba(0, 0, 0, 0.6);
}

.r-lg {
  padding-top: 70px;
  width: 350px;
  border-radius: 5px 35px;
  height: 300px;
  background-color: white;

}

</style>
